package com.software.usercenter.controller;


import cn.hutool.system.UserInfo;
import com.software.usercenter.domain.Result;
import com.software.usercenter.entity.StudentInfo;
import com.software.usercenter.entity.UserDTO;
import com.software.usercenter.service.StudentInfoService;

import com.software.usercenter.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/student_info")
public class StudentInfoController {
    @Autowired
    private StudentInfoService studentInfoService;

    @PostMapping("/addUser")
    public Result<StudentInfo> addUser(
            @RequestBody UserDTO userDTO){
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setUserName(userDTO.getUserName());
        studentInfo.setNickName(userDTO.getNickName());
        studentInfo.setPassword(userDTO.getPassword());
        studentInfo.setUserId("S" + UUID.randomUUID().toString());
        studentInfo.setIcon(userDTO.getIcon());
        studentInfo.setGender(userDTO.getGender());
        studentInfoService.addStudent(studentInfo);
        return ResultUtil.success(studentInfo);
    }


    @PostMapping("/addStudent")
    public Result<StudentInfo> addStudent(
            @RequestParam("param_username") String param_username,
            @RequestParam("param_password") String param_password ,
            @RequestParam("icon")  String icon,
            StudentInfo studentInfo){
        studentInfo.setUserName(param_username);
        studentInfo.setPassword(param_password);
        studentInfo.setUserId("S" + UUID.randomUUID().toString());
        studentInfo.setIcon(icon);
        studentInfoService.addStudent(studentInfo);
        return ResultUtil.success(studentInfo);
    }


    //根据id查询学生
    @GetMapping("/findStudentInfoByUserId/{userId}")
    public StudentInfo findStudentInfoByUserId(@PathVariable String userId){
        StudentInfo find = studentInfoService.findStudentInfoByUserId(userId);
        return find;
    }

    //根据用户名，昵称，性别，模糊查询学生
    @GetMapping("/findStudentInfosByUserNameLikeAndNickNameLikeAndGenderIs")
    public Page<StudentInfo> findStudentInfosByUserNameLikeAndNickNameLikeAndGenderIs(
            @RequestParam(name = "userName", required = false) String userName,
            @RequestParam(name = "nickName", required = false) String nickName,
            @RequestParam(name = "gender", required = false) String gender,
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize){
        Page<StudentInfo> page = studentInfoService.findStudentInfosByUserNameLikeAndNickNameLikeAndGenderIs(userName,nickName,gender, Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    //查询学生
    @GetMapping("/list")
    public Page<StudentInfo> findAll(
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize
    ){
        Page<StudentInfo> page = studentInfoService.findAll(Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    //查询本班学生
    @GetMapping("/findStudentsByClassId/{userId}")
    public List<StudentInfo> findStudentsByClassId(@PathVariable String userId) {
        StudentInfo find = studentInfoService.findStudentInfoByUserId(userId);
        String classId = find.getClassId();
        List<StudentInfo> students = studentInfoService.findStudentsByClassId(classId);
        return students;
    }



    //验证用户名是否存在
    @PostMapping("/checkStudentInfoByUserName")
    public Result<StudentInfo> findStudentInfoByUserName(@RequestParam("param_username") String param_username,StudentInfo studentInfo){
        //判断用户名是否存在
        if(studentInfoService.findStudentInfosByUserName(param_username).size()>0){
            return ResultUtil.error("1","该用户名已存在",studentInfo);
        }
        return ResultUtil.success(studentInfo);
    }
    //验证用户名密码是否正确
    @PostMapping("/findStudentInfoByUserName")
    public Result<StudentInfo> findStudentInfoByUserName(
            @RequestParam("param_username") String param_username,
            @RequestParam("param_password") String param_password){

        List<StudentInfo> studentInfo=studentInfoService.verifyLoginUser(param_username,param_password);
        if(studentInfo.size()>0){
            return ResultUtil.success(studentInfo);
        }else
            return ResultUtil.error("2","用户名密码不匹配",studentInfo);
    }

    //修改学生信息
    @RequestMapping(value = "/updateStudent")
    public void updateStudent(@RequestBody StudentInfo studentInfo){
        studentInfoService.updateStudent(studentInfo);
    }

    //修改密码界面验证密码
    @RequestMapping(value = "/confirmPassword")
    public Result<StudentInfo> confirmPassword(@RequestBody StudentInfo studentInfo){
        List<StudentInfo> list = studentInfoService.confirmPassword(studentInfo);
        if(list.size()>0){
            return ResultUtil.success(studentInfo);
        }else
            return ResultUtil.error("2","用户名密码不匹配",studentInfo);
    }

    //修改密码
    @RequestMapping(value = "/updatePassword")
    public void updatePassword(@RequestBody StudentInfo studentInfo){
        studentInfoService.updatePassword(studentInfo);
    }

    //通过userID删除用户
    @GetMapping(value = "/deleteStudentInfoByUserId/{userId}")
    public void deleteStudentInfoByUserId(@PathVariable String userId){
        studentInfoService.deleteStudentInfoByUserId(userId);
    }

    //删除用户
    @RequestMapping(value = "/deleteStudent")
    public void deleteStudent(@RequestBody StudentInfo studentInfo){
        studentInfoService.deleteStudent(studentInfo);
    }


}
